/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Penjualan;
import model.Produksi;

/**
 *
 * @author elfatah
 */
public class DBPenjualan extends DBClass {

    public List getPenjualanDetails() throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT pen.PENJUALAN_ID, peg.NAMA, bar.KODE_BARANG, pen.JUMLAH_BARANG, pen.JUMLAH_HARGA, pen.TANGGAL  FROM penjualan as pen,pegawai as peg,barang as bar WHERE peg.PEGAWAI_ID = pen.ID_PEGAWAI and bar.BARANG_ID = pen.ID_BARANG");
        ResultSet rs = pstmt.executeQuery();
        List list = new ArrayList();
        while (rs.next()) {
            Penjualan ubean = new Penjualan();
            ubean.setPenjualanId(rs.getString(1));
            ubean.setUsername(rs.getString(2));
            ubean.setKode_barang(rs.getString(3));
            ubean.setJumlahBarang(rs.getString(4));
            ubean.setJumlahHarga(rs.getString(5));
            ubean.setTgl(rs.getString(6));
//            ubean.setHargaSatuan(Integer.parseInt(rs.getString(7)));
//            ubean.setJumlahHarga(rs.getString(8));

            list.add(ubean);

        }
        return list;
    }

    public void insertPenjualan(Penjualan bb) throws SQLException, ClassNotFoundException {
        try {
            Connection con = createConnection();
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO `penjualan`(`ID_PEGAWAI`, `ID_BARANG`, `JUMLAH_BARANG`, `JUMLAH_HARGA`, `TANGGAL`) VALUES (?,?,?,?,?)");

            pstmt.setString(1, bb.getIdPegawai());
            pstmt.setString(2, "2");
            pstmt.setString(3, bb.getJumlahBarang());
            pstmt.setString(4, bb.getJumlahHarga());

            pstmt.setString(5, bb.getTgl());
//    

            pstmt.executeUpdate();

            PreparedStatement pstmt2 = con.prepareStatement("UPDATE `barang` SET `JUMLAH_STOK`=(`JUMLAH_STOK`-?) WHERE `BARANG_ID`=2");
            pstmt2.setString(1, bb.getJumlahBarang());
            pstmt2.setString(2, bb.getIdBarang());
            pstmt2.executeUpdate();
            System.out.println("testing");

        } catch (Exception e) {
            System.out.println(e);

        }
    }

    public void UpdatePenjualan(Penjualan bb, String id) throws SQLException, ClassNotFoundException {

        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("UPDATE `penjualan` SET `ID_PEGAWAI`=?,`ID_BARANG`=?,`JUMLAH_BARANG`=?,`JUMLAH_HARGA`=?,`TANGGAL`=? where `PENJUALAN_ID`=?");
        //set values to prepared statement object by getting values from bean object
        pstmt.setString(1, bb.getIdPegawai());
        pstmt.setString(2, "2");
        //   pstmt.setString(3, bb.getRole());
        pstmt.setString(3, bb.getJumlahBarang());
        pstmt.setString(4, bb.getJumlahHarga());
        pstmt.setString(5, bb.getTgl());
        pstmt.setString(6, id);
        pstmt.executeUpdate();

    }

    public void deletePenjualan(String id) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("delete from penjualan where `PENJUALAN_ID`=?");
        pstmt.setString(1, id);
        pstmt.executeUpdate();
    }

    public Penjualan getPenjualanDetails(String id) throws SQLException, ClassNotFoundException {
        //here we will write code to get a single record from database
        Connection con = createConnection();
        PreparedStatement pstmt = con.prepareStatement("SELECT pen.PENJUALAN_ID, peg.NAMA, bar.KODE_BARANG, pen.JUMLAH_BARANG, pen.JUMLAH_HARGA, pen.TANGGAL, peg.PEGAWAI_ID, bar.BARANG_ID FROM penjualan as pen,pegawai as peg,barang as bar WHERE peg.PEGAWAI_ID = pen.ID_PEGAWAI and bar.BARANG_ID = pen.ID_BARANG and `PENJUALAN_ID`=?");
        pstmt.setString(1, id);
        ResultSet rs = pstmt.executeQuery();

        Penjualan ubean = new Penjualan();
        while (rs.next()) {
            ubean.setPenjualanId(rs.getString(1));
            ubean.setNama(rs.getString(2));
            ubean.setKode_barang(rs.getString(3));
            ubean.setJumlahBarang(rs.getString(4));
            ubean.setJumlahHarga(rs.getString(5));
            ubean.setTgl(rs.getString(6));
             ubean.setIdPegawai(rs.getString(7));
             ubean.setIdBarang(rs.getString(8));

        }
        ubean.setAction("Update");
        return ubean;
    }

}
